export default{
	namespaced:true,
	
	state:()=>({
		// 新消息更新标识
		flag: false,
		
		// 聊天列表（历史记录）
		chatHistory:[
			{
			name:"张三", 
			// 所有内容
			chatContents:[
				{content:"在吗？", date:"2023-05-12", who:"other"},
				{content:"你好", date:"2023-05-12", who:"other"},
			],
			// 未读数
			unreadNumber:2
			},
			{
			name:"李四", 
			// 所有内容
			chatContents:[
				{content:"你好", date:"2023-05-12", who:"other"},
			],
			// 未读数
			unreadNumber:1
			}
		]
	}),
	
	mutations: {
		// 修改更新标识
		setFlag(state, value) {
			console.log("修改falg值为：", value)
			state.flag = value;
		},
		// 添加聊天列表（记录）
		// chatObj=>{name:"用户姓名"，content："内容", who:"角色"}
		addChatList(state, chatObj){
			let chatList = state.chatHistory;
			// 判断用户是否存在展示列表中
			let existFlag = false;
			// 存在索引位置
			let exisIndex = 999;
			for(let i = 0; i < chatList.length; i++){
				if(chatList[i].name == chatObj.name){
					existFlag = true;
					exisIndex = i;
				}
			}
			let currentDate = new Date();
			if(!existFlag){
				// 构造chatContents，存储聊天记录,判定为第一条聊天记录
				const historyTemp = [];
				historyTemp.unshift({content: chatObj.content, date: currentDate, who:chatObj.who})
				chatList.push({name:chatObj.name, chatContents:historyTemp, unreadNumber:"1"})
			}else{
				// 用户存在聊天列表，添加聊天记录
				// 聊天聊天记录对象
				if(exisIndex != 999)
					chatList[exisIndex].chatContents.unshift({content: chatObj.content, date: currentDate, who:chatObj.who}) 
			}
		},
		
	},
	
	getters: {
		// 获取聊天对象
		getChatObj: (state) => (name) => {
		    console.log(name);
		    let chatList = state.chatHistory;
		    console.log(chatList);
		    for(let i = 0; i < chatList.length; i++){
		      if(chatList[i].name == name){
		        return JSON.stringify(chatList[i]);
		      }
		    }
		  }
	}
	
}